package sample.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import sample.model.ExtremeData;
import sample.model.StandardData;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ParseExcel {
    private static FileInputStream inputStream;

    public static List<StandardData> parseStandardExcel(String path) {
        Workbook readWorkbookType = getReadWorkbookType(path);
        List<StandardData> list = readExcel(readWorkbookType);
        return list;
    }

    public static List<ExtremeData> parseExtremeExcel(String path) {
        Workbook readWorkbookType = getReadWorkbookType(path);
        List<ExtremeData> list = readExtremeExcel(readWorkbookType);
        return list;

    }

    public static Workbook getReadWorkbookType(String path) {
        try {
            inputStream = new FileInputStream(path);
            if (path.toLowerCase().endsWith(".xlsx")) {
                return new XSSFWorkbook(inputStream);
            }
            if (path.toLowerCase().endsWith("xls"))
                return new HSSFWorkbook(inputStream);
            else
                throw new Exception("excel格式文件错误");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static List<StandardData> readExcel(Workbook workbook) {
        Sheet sheetAt = workbook.getSheetAt(0);
        int lastRowNum = sheetAt.getLastRowNum();
        List<StandardData> list = new ArrayList<>();
//        ExecutorService executorService = Executors.newFixedThreadPool(5);
//        ReentrantLock lock = new ReentrantLock();
////        Semaphore semaphore = new Semaphore(8);
//        AtomicInteger atomicInteger = new AtomicInteger(1);
        long l = System.currentTimeMillis();
//        for (; atomicInteger.get() < lastRowNum;) {
//            try {
//                semaphore.acquire(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            executorService.submit(() -> {
//                lock.lock();
//                try {
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheetAt.getRow(i);
            StandardData standardData = new StandardData(
                    (int) (row.getCell(0).getNumericCellValue()),
                    (int) (row.getCell(1).getNumericCellValue()),
                    (int) (row.getCell(2).getNumericCellValue()),
                    (int) (row.getCell(3).getNumericCellValue()),
                    row.getCell(4).getNumericCellValue(),
                    row.getCell(5).getNumericCellValue(),
                    row.getCell(6).getNumericCellValue(),
                    row.getCell(7).getNumericCellValue(),
                    row.getCell(8).getNumericCellValue(),
                    row.getCell(9).getNumericCellValue(),
                    (int) row.getCell(10).getNumericCellValue(),
                    row.getCell(11).getNumericCellValue(),
                    row.getCell(12).getNumericCellValue(),
                    row.getCell(13).getNumericCellValue(),
                    (int)row.getCell(14).getNumericCellValue()
            );
            list.add(standardData);

        }
//                    list.add(standardData);
//                    atomicInteger.getAndIncrement();
//                }finally {
//                    lock.unlock();
//                }
//            });
//            semaphore.release();
//        }
        System.out.println(System.currentTimeMillis() - l);
        return list;
    }

    private static List<ExtremeData> readExtremeExcel(Workbook workbook) {
        Sheet sheet = workbook.getSheetAt(0);
        List<ExtremeData> list = new ArrayList<>();
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            ExtremeData extremeData = new ExtremeData(
                    (int) (row.getCell(0).getNumericCellValue()),
                    (row.getCell(1).getNumericCellValue()),
                    (row.getCell(2).getNumericCellValue()),
                    (row.getCell(3).getNumericCellValue()),
                    (int) row.getCell(4).getNumericCellValue(),
                    (int) row.getCell(5).getNumericCellValue(),
                    row.getCell(6).getNumericCellValue(),
                    row.getCell(7).getNumericCellValue(),
                    row.getCell(8).getNumericCellValue(),
                    row.getCell(9).getNumericCellValue(),
                    (int) row.getCell(10).getNumericCellValue(),
                    row.getCell(11).getNumericCellValue(),
                    row.getCell(12).getNumericCellValue(),
                    row.getCell(13).getNumericCellValue(),
                    row.getCell(14).getNumericCellValue(),
                    row.getCell(15).getNumericCellValue(),
                    row.getCell(16).getNumericCellValue(),
                    row.getCell(17).getNumericCellValue(),
                    row.getCell(18).getNumericCellValue(),
                    row.getCell(19).getNumericCellValue(),
                    row.getCell(20).getNumericCellValue(),
                    row.getCell(21).getNumericCellValue(),
                    row.getCell(22).getNumericCellValue(),
                    row.getCell(23).getNumericCellValue(),
                    row.getCell(24).getNumericCellValue()
            );
            list.add(extremeData);
        }
        return list;
    }

//    public static void main(String[] args) throws MalformedURLException {
//        String path = "/home/hankun/Desktop/jianzhu.xlsx";
////        URL url = new File("file:/home/hankun/Desktop/jianzhu.xlsx").toURI().toURL();
//        parseStandardExcel(path);
//    }
}
